﻿using CoreShop.Library;
using Sunny.UI;
using System.Collections.Concurrent;
using System.Windows.Forms.VisualStyles;

namespace CoreShop.PrintService
{
    public partial class frmBillTemplateEdit : UIEditForm
    {
        public PageSize PageSize;
        public int billtypeId = 1;

        public bool MultiLine
        {
            get => cbSingle.Checked;
            set => cbSingle.Checked = value;
        }

        private readonly VerticalAlignment VerAlign;

        public frmBillTemplateEdit(PageSize pageSize, VerticalAlignment align,int _billtypeId)
        {
            InitializeComponent();

            Items.TryAdd(0, e0);
            Items.TryAdd(1, e1);
            Items.TryAdd(2, e2);
            Items.TryAdd(3, e3);
            Items.TryAdd(4, e4);
            Items.TryAdd(5, e5);
            billtypeId = _billtypeId;
            PageSize = pageSize;
            VerAlign = align;
            cbSingle.Visible = align == VerticalAlignment.Center;
            rbData.Checked = true;
            rbData.Visible = rbBlank.Visible = rbLine.Visible = align != VerticalAlignment.Center;

            foreach (var editor in Items.Values)
            {
                editor.Init(billtypeId);
                editor.VerAlign = align;
            }

            edtFontSize.SelectedIndex = 0;
            cbColumn.SelectedIndex = 0;
        }

        protected override bool CheckData()
        {
            if (!rbData.Checked) return true;

            if (CommFunc.ConvertToInt32(cbColumn.Text) == 1)
            {
                e0.Weight = TotalCount;
            }

            int cnt = 0;
            foreach (var editor in Items.Values)
            {
                if (editor.Enabled)
                {
                    cnt += editor.Weight;
                    if (!editor.Check(rbBlank.Checked))
                        return false;
                }
            }

            if (cnt != TotalCount)
            {
                ShowWarningTip("字符个数与可用字符个数不等，请检查。");
                return false;
            }

            return base.CheckData();
        }

        public ConcurrentDictionary<int, UIEditor> Items = new ConcurrentDictionary<int, UIEditor>();

        private MenuLine line;

        public void SetLine(MenuLine value)
        {
            if (value == null) return;
            line = value;

            edtFontSize.SelectedIndex = line.FontSize - 1;
            cbColumn.SelectedIndex = line.Items.Count - 1;

            if (value.VerAlign != VerticalAlignment.Center)
            {
                rbData.Checked = true;
                if (line.IsLine) rbLine.Checked = true;
                if (line.IsBlank) rbBlank.Checked = true;
            }

            for (int i = 0; i < line.Items.Count; i++)
            {
                Items[i].SetItem(line.Items[i]);
            }
        }

        public MenuLine GetLine()
        {
            if (line == null) line = new MenuLine();
            line.VerAlign = VerAlign;

            if (rbLine.Checked)
            {
                line.IsLine = true;
                return line;
            }

            if (rbBlank.Checked)
            {
                line.IsBlank = true;
                return line;
            }

            line.FontSize = edtFontSize.SelectedIndex + 1;
            line.Items.Clear();

            for (int i = 0; i < 6; i++)
            {
                if (cbColumn.SelectedIndex >= i) line.Items.Add(Items[i].GetItem());
            }

            return line;
        }

        private void cbColumn_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            for (int i = 0; i < 6; i++)
            {
                Items[i].Enabled = rbData.Checked && cbColumn.SelectedIndex >= i;
            }

            lblCount.Text = "可用字符个数为：" + TotalCount;
        }

        private void edtFontSize_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            lblCount.Text = "可用字符个数为：" + TotalCount;
        }

        private int TotalCount => PageSize.CharCount(CommFunc.ConvertToInt32(edtFontSize.Text)) - cbColumn.SelectedIndex;

        private void rbData_ValueChanged(object sender, bool value)
        {
            cbColumn_SelectedIndexChanged(null, null);
        }

        private void rbBlank_ValueChanged(object sender, bool value)
        {
            cbColumn_SelectedIndexChanged(null, null);
        }

        private void rbLine_ValueChanged(object sender, bool value)
        {
            cbColumn_SelectedIndexChanged(null, null);
        }
    }
}
